package com.china.lottery.listener.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.china.lottery.listener.dao.base.CommonDao;
import com.china.lottery.listener.dao.entity.LDictionary;
import com.china.lottery.listener.util.Tools;

@Repository("dictionaryDao")
public class DictionaryDao extends CommonDao {

	@SuppressWarnings("unchecked")
	public LDictionary findByCode(String code) {
		String jpql = "select t from LDictionary t where code = :code ";
		Query q = em.createQuery(jpql);
		q.setParameter("code", code);
		q.setMaxResults(1);
		q.setFirstResult(0);
		List<LDictionary> dics = q.getResultList();
		if (Tools.isBlank(dics)) {
			return null;
		}
		return dics.get(0);
	}

	@SuppressWarnings("unchecked")
	// @Cacheable(value = "dicCache", key = "'findByParentCode'+#code")
	public List<LDictionary> findByParentCode(String code) {
		String jpql = "select t from LDictionary t where t.parent in (select d.id from LDictionary d where d.code= :code)";
		Query q = em.createQuery(jpql);
		q.setParameter("code", code);
		List<LDictionary> dics = q.getResultList();
		if (Tools.isBlank(dics)) {
			return null;
		}
		return dics;
	}
}
